package com.hd.controller.job;

import java.util.HashMap;
import java.util.Map;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ParseException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.controller.base.BaseController;
import com.hd.entity.job.QuartzScheduleJob;
import com.hd.service.job.QuartzScheduleJobService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
/**
 * 定时任务管理
 * @author hzhh123
 *
 */
@Controller
@RequestMapping("quartzScheduleJob")
public class QuartzScheduleJobController extends BaseController{
	@Autowired
	private QuartzScheduleJobService quartzScheduleJobService;
	 /**
     * 主页
     * @return
     */
    @RequestMapping("manager")
    public String manager() {
        return "jsp/quartzScheduleJob/quartzScheduleJobList";
    }
    /**
     * 列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/dataGrid",method=RequestMethod.POST)
    @ResponseBody
    public Object dataGrid(QuartzScheduleJob scheduleJob, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        if(StringUtils.isNotNull(scheduleJob.getJobName())) {
        	condition.put("jobName", scheduleJob.getJobName());
        }
        pageInfo.setCondition(condition);
        quartzScheduleJobService.selectDataGrid(pageInfo);
        return pageInfo;
    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "jsp/quartzScheduleJob/quartzScheduleJobAdd";
    }

    /**
     * 添加
     * @param quartzScheduleJob
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(QuartzScheduleJob quartzScheduleJob) {
    	if(StringUtils.isNotNull(quartzScheduleJob.getRunDate())){
    		try {
				quartzScheduleJobService.addSimpleJob(quartzScheduleJob.getJobName(),
						quartzScheduleJob.getJobGroup(), quartzScheduleJob.getClassName(), 
						quartzScheduleJob.getMethodName(), quartzScheduleJob.getParams(), 
						quartzScheduleJob.getRunDate(), quartzScheduleJob.getDesc());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	if(StringUtils.isNotNull(quartzScheduleJob.getCron())){
    		try {
				quartzScheduleJobService.addCronJob(quartzScheduleJob.getJobName(), 
						quartzScheduleJob.getJobGroup(), quartzScheduleJob.getClassName(), 
						quartzScheduleJob.getMethodName(), quartzScheduleJob.getParams(), 
						quartzScheduleJob.getCron(), quartzScheduleJob.getDesc());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
        return renderSuccess("添加成功！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(String jobName,String jobGroup) {
        try {
			quartzScheduleJobService.deleteJob(jobName,jobGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return renderSuccess("删除成功！");
    }
    /**
     * 删除所有任务
     * @return
     */
    @RequestMapping(value = "/deleteBatch",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteBatch() {
    	try {
    		quartzScheduleJobService.deleteAll();
    	} catch (SchedulerException e) {
    		e.printStackTrace();
    	}
    	return renderSuccess("删除成功！");
    }
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage/{jobName}")
	public String editPage(Model model, @PathVariable("jobName")String jobName) {
    	QuartzScheduleJob quartzScheduleJob = quartzScheduleJobService.get(jobName);
        model.addAttribute("quartzScheduleJob", quartzScheduleJob);
        return "jsp/quartzScheduleJob/quartzScheduleJobEdit";
    }

    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/viewPage/{jobName}")
    public String viewPage(Model model, @PathVariable("jobName") String jobName) {
    	QuartzScheduleJob quartzScheduleJob = quartzScheduleJobService.get(jobName);
        model.addAttribute("quartzScheduleJob", quartzScheduleJob);
        return "jsp/quartzScheduleJob/quartzScheduleJobView";
    }
    /**
     * 编辑
     * @param quartzScheduleJob
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(QuartzScheduleJob quartzScheduleJob) {
    	if(StringUtils.isNotNull(quartzScheduleJob.getRunDate())){
    		try {
				quartzScheduleJobService.updateSimpleJob(quartzScheduleJob.getJobName(), 
						quartzScheduleJob.getJobGroup(), quartzScheduleJob.getRunDate());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	if(StringUtils.isNotNull(quartzScheduleJob.getCron())){
    		try {
				quartzScheduleJobService.updateCronJob(quartzScheduleJob.getJobName(), 
						quartzScheduleJob.getJobGroup(), quartzScheduleJob.getCron());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
        return renderSuccess("编辑成功！");
    }

    /**
     * 暂停定时任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @ResponseBody
    @RequestMapping("/pauseJob")
    public Object pauseJob(String jobName,String jobGroup) {
    	try {
			quartzScheduleJobService.pauseJob(jobName,jobGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return renderSuccess("任务暂停成功！");
    }
    
    /**
     * 重启定时任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @ResponseBody
    @RequestMapping("/resumeJob")
    public Object resumeJob(String jobName,String jobGroup) {
    	try {
			quartzScheduleJobService.resumeJob(jobName,jobGroup);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return renderSuccess("任务启动成功！");
    }

}
